import { openUrl } from '@/utils'
import ActionConfig from './config'

export default {
  methods: {
    onAction(action) {
      console.log('触发action:', action)
      if (/^d2-menu-empty-\d+$/.test(action) || action === undefined) {
        // d2-menu-empty-数字  视为内部的临时菜单,比如: d2-menu-empty-123
        this.$message.warning('临时菜单')
      } else if (/^https:\/\/|http:\/\//.test(action)) {
        // http或https开头的视为url网址
        openUrl(action)
      } else if (/^\/[A-Za-z0-9]+/.test(action)) {
        // 斜杆开头的都视为vue的router,比如：/path /index /home/analyse
        this.$router.push({ path: action })
      } else {
        // 自定义action
        this.onCustomAction(action)
      }
    },
    onCustomAction(action) {
      const handle = ActionConfig[action] || null
      if (handle !== null || handle !== undefined) {
        let args = null
        if (handle.hasOwnProperty('args')) {
          args = handle.args
        }
        // this.$message({
        //   message:
        //     '触发命令:' +
        //     action +
        //     '  处理动作:' +
        //     handle.action +
        //     '  参数:' +
        //     args,
        //   showClose: true,
        //   type: 'success'
        //   // duration: 1000
        // })
        this.$store.dispatch(handle.action, args)
      } else {
        this.$message({
          message: '【提示】未注册 ' + action + ' 处理程序!',
          showClose: true,
          type: 'error'
          // duration: 1000
        })
      }
    }
  }
}
